//
//  AKUserDefaults.m
//  YingkeWlanIOS
//
//  Created by FranklinPhill on 15/8/7.
//  Copyright (c) 2015年 FranklinPhill. All rights reserved.
//

#import "AKUserDefaults.h"
#import "SSKeychain.h"

@implementation AKUserDefaults

+ (id)sharedInstance {
    static id sharedInstance = nil;
    if (nil == sharedInstance) {
        @synchronized([AKUserDefaults class]) {
            if (nil == sharedInstance) {
                sharedInstance = [[AKUserDefaults alloc] init];
            }
        }
    }
    return sharedInstance;
}

+ (void)synchronize {
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (void)registerUserDefaults {
    DLog(@"registerUserDefaults");
    AKUserDefaults *defaults = [AKUserDefaults sharedInstance];
    NSString *settingVersion = [defaults versionString];
    DLog(@"[CBUserDefaults sharedInstance] versionString is %@, defaults versionString is %@", APP_VERSION, settingVersion);
    if (nil == settingVersion) {    //初次安装app
        [[AKUserDefaults sharedInstance] setVersionString:APP_VERSION];
        defaults.state = AKAppStateFirstUse;
        [[AKClient sharedInstance] clearKeychain];  //有可能是删除了APP以后再安装, 所以这里清一次keychain
    }
    else if (![APP_VERSION isEqualToString:settingVersion]) {  //app升级
        [[AKUserDefaults sharedInstance] setVersionString:APP_VERSION];
        [[AKClient sharedInstance] clearKeychain];  //升级以后清keychain
        defaults.state = AKAppStateUpgrade;
        //升级以后清除所有webview缓存
        [[NSURLCache  sharedURLCache] removeAllCachedResponses];
        //清除cookies
        NSHTTPCookie *cookie;
        NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
        for (cookie in [storage cookies]) {
            [storage deleteCookie:cookie];
        }
    }
    else {
        defaults.state = AKAppStateNormal;
    }
    [AKUserDefaults synchronize];
}

- (NSString *)versionString {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"kVersionString"];
}

- (void)setVersionString:(NSString *)versionStr {
    [[NSUserDefaults standardUserDefaults] setValue:versionStr forKey:@"kVersionString"];
}

- (NSString *)deviceid {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"kDeviceId"];
}

- (void)setDeviceid:(NSString *)deviceid {
    [[NSUserDefaults standardUserDefaults] setValue:deviceid forKey:@"kDeviceId"];
}

- (NSString *)apMsg {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"kApMsg"];
}

- (void)setApMsg:(NSString *)apMsg {
    [[NSUserDefaults standardUserDefaults] setValue:apMsg forKey:@"kApMsg"];
}

- (NSString *)token {
    return [SSKeychain passwordForService:KEYCHAIN_SERVICE account:@"kToken"];
}

- (void)setToken:(NSString *)token {
    [SSKeychain setPassword:token forService:KEYCHAIN_SERVICE account:@"kToken"];
}

- (NSString *)dc {
    return [SSKeychain passwordForService:KEYCHAIN_SERVICE account:@"kDc"];
}

- (void)setDc:(NSString *)dc {
    [SSKeychain setPassword:dc forService:KEYCHAIN_SERVICE account:@"kDc"];
}

- (NSString *)phone {
    return [SSKeychain passwordForService:KEYCHAIN_SERVICE account:@"kPhone"];
}

- (void)setPhone:(NSString *)phone {
    [SSKeychain setPassword:phone forService:KEYCHAIN_SERVICE account:@"kPhone"];
}

- (NSString *)pwd {
    return [SSKeychain passwordForService:KEYCHAIN_SERVICE account:@"kPwd"];
}

- (void)setPwd:(NSString *)pwd {
    [SSKeychain setPassword:pwd forService:KEYCHAIN_SERVICE account:@"kPwd"];
}

@end
